
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>AJAX · JavaScript学习</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="Veng">
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-pageview-count/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-back-to-top-button/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-code/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="Canvas.html" />
    
    
    <link rel="prev" href="操作文件.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../基础/基础.html">
            
                <a href="../基础/基础.html">
            
                    
                    基础
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="../基础/变量.html">
            
                <a href="../基础/变量.html">
            
                    
                    数据类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="../基础/let和const命令.html">
            
                <a href="../基础/let和const命令.html">
            
                    
                    let和const命令
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="../基础/数值.html">
            
                <a href="../基础/数值.html">
            
                    
                    数值
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="../基础/字符串.html">
            
                <a href="../基础/字符串.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="../基础/对象.html">
            
                <a href="../基础/对象.html">
            
                    
                    对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="../基础/数组.html">
            
                <a href="../基础/数组.html">
            
                    
                    数组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="../基础/Symbol.html">
            
                <a href="../基础/Symbol.html">
            
                    
                    Symbol
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8" data-path="../基础/函数.html">
            
                <a href="../基础/函数.html">
            
                    
                    函数
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.8.1" data-path="../基础/函数的扩展.html">
            
                <a href="../基础/函数的扩展.html">
            
                    
                    函数的扩展
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8.2" data-path="../基础/变量的解构赋值.html">
            
                <a href="../基础/变量的解构赋值.html">
            
                    
                    变量作用域与解构赋值
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8.3" data-path="../基础/函数3.html">
            
                <a href="../基础/函数3.html">
            
                    
                    方法函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8.4" data-path="../基础/高阶函数.html">
            
                <a href="../基础/高阶函数.html">
            
                    
                    高阶函数
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.8.4.1" data-path="../基础/map-reduce.html">
            
                <a href="../基础/map-reduce.html">
            
                    
                    map/reduce
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8.4.2" data-path="../基础/filter.html">
            
                <a href="../基础/filter.html">
            
                    
                    filter
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8.4.3" data-path="../基础/sort.html">
            
                <a href="../基础/sort.html">
            
                    
                    sort
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8.4.4" data-path="../基础/array其他方法.html">
            
                <a href="../基础/array其他方法.html">
            
                    
                    array其他方法
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.8.5" data-path="../基础/闭包.html">
            
                <a href="../基础/闭包.html">
            
                    
                    闭包
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8.6" data-path="../基础/箭头函数.html">
            
                <a href="../基础/箭头函数.html">
            
                    
                    箭头函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8.7" data-path="Promise.html">
            
                <a href="Promise.html">
            
                    
                    Promise
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8.8" data-path="../基础/generator.html">
            
                <a href="../基础/generator.html">
            
                    
                    generator
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8.9" data-path="../基础/async.html">
            
                <a href="../基础/async.html">
            
                    
                    async
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.9" data-path="../基础/标准对象.html">
            
                <a href="../基础/标准对象.html">
            
                    
                    标准对象
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.9.1" data-path="../基础/SetMap.html">
            
                <a href="../基础/SetMap.html">
            
                    
                    Set,Map
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9.2" data-path="../基础/Date.html">
            
                <a href="../基础/Date.html">
            
                    
                    Date
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9.3" data-path="../基础/Math.html">
            
                <a href="../基础/Math.html">
            
                    
                    Math
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9.4" data-path="../基础/Proxy.html">
            
                <a href="../基础/Proxy.html">
            
                    
                    Proxy
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9.5" data-path="../基础/RepExp.html">
            
                <a href="../基础/RepExp.html">
            
                    
                    正则表达式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9.6" data-path="../基础/正则表达式.html">
            
                <a href="../基础/正则表达式.html">
            
                    
                    正则表达式别篇
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.9.7" data-path="../基础/json.html">
            
                <a href="../基础/json.html">
            
                    
                    JSON
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.10" data-path="../基础/面向对象编程.html">
            
                <a href="../基础/面向对象编程.html">
            
                    
                    面向对象编程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.10.1" data-path="../基础/对象的扩展.html">
            
                <a href="../基础/对象的扩展.html">
            
                    
                    对象的扩展
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.10.2" data-path="../基础/创建对象.html">
            
                <a href="../基础/创建对象.html">
            
                    
                    创建对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.10.3" data-path="../基础/原型继承.html">
            
                <a href="../基础/原型继承.html">
            
                    
                    原型继承
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.10.4" data-path="../基础/class继承.html">
            
                <a href="../基础/class继承.html">
            
                    
                    class继承
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.10.5" data-path="../基础/模块化Module.html">
            
                <a href="../基础/模块化Module.html">
            
                    
                    模块化Module
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.2.11" data-path="../基础/异常处理.html">
            
                <a href="../基础/异常处理.html">
            
                    
                    异常处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.12" data-path="../基础/编程风格.html">
            
                <a href="../基础/编程风格.html">
            
                    
                    编程风格
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../js扩展/js扩展.html">
            
                <a href="../js扩展/js扩展.html">
            
                    
                    js 扩展
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../js扩展/字符串.html">
            
                <a href="../js扩展/字符串.html">
            
                    
                    字符串
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="浏览器.html">
            
                <a href="浏览器.html">
            
                    
                    浏览器
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="浏览器对象.html">
            
                <a href="浏览器对象.html">
            
                    
                    浏览器对象
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="操作DOM.html">
            
                <a href="操作DOM.html">
            
                    
                    操作DOM
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.2.1" data-path="更新DOM.html">
            
                <a href="更新DOM.html">
            
                    
                    更新DOM
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2.2" data-path="插入DOM.html">
            
                <a href="插入DOM.html">
            
                    
                    插入DOM
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2.3" data-path="删除DOM.html">
            
                <a href="删除DOM.html">
            
                    
                    删除DOM
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="DOM事件.html">
            
                <a href="DOM事件.html">
            
                    
                    DOM事件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="操作表单.html">
            
                <a href="操作表单.html">
            
                    
                    操作表单
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="操作文件.html">
            
                <a href="操作文件.html">
            
                    
                    操作文件
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.4.6" data-path="AJAX.html">
            
                <a href="AJAX.html">
            
                    
                    AJAX
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="Canvas.html">
            
                <a href="Canvas.html">
            
                    
                    Canvas
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../Jquery/jquery.html">
            
                <a href="../Jquery/jquery.html">
            
                    
                    Jquery
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../Jquery/选择器.html">
            
                <a href="../Jquery/选择器.html">
            
                    
                    选择器
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1.1" data-path="../Jquery/层级选择器.html">
            
                <a href="../Jquery/层级选择器.html">
            
                    
                    层级选择器
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.1.2" data-path="../Jquery/查找和过滤.html">
            
                <a href="../Jquery/查找和过滤.html">
            
                    
                    查找和过滤
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../Jquery/操作DOM.html">
            
                <a href="../Jquery/操作DOM.html">
            
                    
                    操作DOM
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.2.1" data-path="../Jquery/修改DOM结构.html">
            
                <a href="../Jquery/修改DOM结构.html">
            
                    
                    修改DOM结构
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../Jquery/事件.html">
            
                <a href="../Jquery/事件.html">
            
                    
                    事件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../Jquery/动画.html">
            
                <a href="../Jquery/动画.html">
            
                    
                    动画
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../Jquery/AJAX.html">
            
                <a href="../Jquery/AJAX.html">
            
                    
                    AJAX
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="../Jquery/扩展.html">
            
                <a href="../Jquery/扩展.html">
            
                    
                    扩展
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../nodejs/nodejs.html">
            
                <a href="../nodejs/nodejs.html">
            
                    
                    Nodejs
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../nodejs/模块.html">
            
                <a href="../nodejs/模块.html">
            
                    
                    模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../nodejs/path.html">
            
                <a href="../nodejs/path.html">
            
                    
                    path
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../nodejs/文件操作.html">
            
                <a href="../nodejs/文件操作.html">
            
                    
                    文件操作 fs
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.4" data-path="../nodejs/流Stream.html">
            
                <a href="../nodejs/流Stream.html">
            
                    
                    流Stream
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.5" data-path="../nodejs/zlib压缩解压缩.html">
            
                <a href="../nodejs/zlib压缩解压缩.html">
            
                    
                    zlib压缩解压缩
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.6" data-path="../nodejs/net网络.html">
            
                <a href="../nodejs/net网络.html">
            
                    
                    net网络
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.7" data-path="../nodejs/http.html">
            
                <a href="../nodejs/http.html">
            
                    
                    http
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.8" data-path="../nodejs/express框架.html">
            
                <a href="../nodejs/express框架.html">
            
                    
                    express框架
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.9" data-path="../nodejs/process进程.html">
            
                <a href="../nodejs/process进程.html">
            
                    
                    process 进程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.10" data-path="../nodejs/child_process.html">
            
                <a href="../nodejs/child_process.html">
            
                    
                    child_process
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.11" data-path="../nodejs/os.html">
            
                <a href="../nodejs/os.html">
            
                    
                    os-操作系统
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../react/react.html">
            
                <a href="../react/react.html">
            
                    
                    React
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >AJAX</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="ajax">AJAX</h1>
<p>AJAX&#x4E0D;&#x662F;JavaScript&#x7684;&#x89C4;&#x8303;&#xFF0C;&#x5B83;&#x53EA;&#x662F;&#x4E00;&#x4E2A;&#x54E5;&#x4EEC;&#x201C;&#x53D1;&#x660E;&#x201D;&#x7684;&#x7F29;&#x5199;&#xFF1A;Asynchronous JavaScript and XML&#xFF0C;&#x610F;&#x601D;&#x5C31;&#x662F;&#x7528;JavaScript&#x6267;&#x884C;&#x5F02;&#x6B65;&#x7F51;&#x7EDC;&#x8BF7;&#x6C42;&#x3002;</p>
<p>&#x5982;&#x679C;&#x4ED4;&#x7EC6;&#x89C2;&#x5BDF;&#x4E00;&#x4E2A;Form&#x7684;&#x63D0;&#x4EA4;&#xFF0C;&#x4F60;&#x5C31;&#x4F1A;&#x53D1;&#x73B0;&#xFF0C;&#x4E00;&#x65E6;&#x7528;&#x6237;&#x70B9;&#x51FB;&#x201C;Submit&#x201D;&#x6309;&#x94AE;&#xFF0C;&#x8868;&#x5355;&#x5F00;&#x59CB;&#x63D0;&#x4EA4;&#xFF0C;&#x6D4F;&#x89C8;&#x5668;&#x5C31;&#x4F1A;&#x5237;&#x65B0;&#x9875;&#x9762;&#xFF0C;&#x7136;&#x540E;&#x5728;&#x65B0;&#x9875;&#x9762;&#x91CC;&#x544A;&#x8BC9;&#x4F60;&#x64CD;&#x4F5C;&#x662F;&#x6210;&#x529F;&#x4E86;&#x8FD8;&#x662F;&#x5931;&#x8D25;&#x4E86;&#x3002;&#x5982;&#x679C;&#x4E0D;&#x5E78;&#x7531;&#x4E8E;&#x7F51;&#x7EDC;&#x592A;&#x6162;&#x6216;&#x8005;&#x5176;&#x4ED6;&#x539F;&#x56E0;&#xFF0C;&#x5C31;&#x4F1A;&#x5F97;&#x5230;&#x4E00;&#x4E2A;404&#x9875;&#x9762;&#x3002;</p>
<p>&#x8FD9;&#x5C31;&#x662F;Web&#x7684;&#x8FD0;&#x4F5C;&#x539F;&#x7406;&#xFF1A;&#x4E00;&#x6B21;HTTP&#x8BF7;&#x6C42;&#x5BF9;&#x5E94;&#x4E00;&#x4E2A;&#x9875;&#x9762;&#x3002;</p>
<p>&#x5982;&#x679C;&#x8981;&#x8BA9;&#x7528;&#x6237;&#x7559;&#x5728;&#x5F53;&#x524D;&#x9875;&#x9762;&#x4E2D;&#xFF0C;&#x540C;&#x65F6;&#x53D1;&#x51FA;&#x65B0;&#x7684;HTTP&#x8BF7;&#x6C42;&#xFF0C;&#x5C31;&#x5FC5;&#x987B;&#x7528;JavaScript&#x53D1;&#x9001;&#x8FD9;&#x4E2A;&#x65B0;&#x8BF7;&#x6C42;&#xFF0C;&#x63A5;&#x6536;&#x5230;&#x6570;&#x636E;&#x540E;&#xFF0C;&#x518D;&#x7528;JavaScript&#x66F4;&#x65B0;&#x9875;&#x9762;&#xFF0C;&#x8FD9;&#x6837;&#x4E00;&#x6765;&#xFF0C;&#x7528;&#x6237;&#x5C31;&#x611F;&#x89C9;&#x81EA;&#x5DF1;&#x4ECD;&#x7136;&#x505C;&#x7559;&#x5728;&#x5F53;&#x524D;&#x9875;&#x9762;&#xFF0C;&#x4F46;&#x662F;&#x6570;&#x636E;&#x5374;&#x53EF;&#x4EE5;&#x4E0D;&#x65AD;&#x5730;&#x66F4;&#x65B0;&#x3002;</p>
<p>&#x6700;&#x65E9;&#x5927;&#x89C4;&#x6A21;&#x4F7F;&#x7528;AJAX&#x7684;&#x5C31;&#x662F;Gmail&#xFF0C;Gmail&#x7684;&#x9875;&#x9762;&#x5728;&#x9996;&#x6B21;&#x52A0;&#x8F7D;&#x540E;&#xFF0C;&#x5269;&#x4E0B;&#x7684;&#x6240;&#x6709;&#x6570;&#x636E;&#x90FD;&#x4F9D;&#x8D56;&#x4E8E;AJAX&#x6765;&#x66F4;&#x65B0;&#x3002;</p>
<p>&#x7528;JavaScript&#x5199;&#x4E00;&#x4E2A;&#x5B8C;&#x6574;&#x7684;AJAX&#x4EE3;&#x7801;&#x5E76;&#x4E0D;&#x590D;&#x6742;&#xFF0C;&#x4F46;&#x662F;&#x9700;&#x8981;&#x6CE8;&#x610F;&#xFF1A;AJAX&#x8BF7;&#x6C42;&#x662F;&#x5F02;&#x6B65;&#x6267;&#x884C;&#x7684;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#xFF0C;&#x8981;&#x901A;&#x8FC7;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x83B7;&#x5F97;&#x54CD;&#x5E94;&#x3002;</p>
<p>&#x5728;&#x73B0;&#x4EE3;&#x6D4F;&#x89C8;&#x5668;&#x4E0A;&#x5199;AJAX&#x4E3B;&#x8981;&#x4F9D;&#x9760;<code>XMLHttpRequest</code>&#x5BF9;&#x8C61;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">success</span>(<span class="hljs-params">text</span>) </span>{
    <span class="hljs-keyword">var</span> textarea = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">&apos;test-response-text&apos;</span>);
    textarea.value = text;
}

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fail</span>(<span class="hljs-params">code</span>) </span>{
    <span class="hljs-keyword">var</span> textarea = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">&apos;test-response-text&apos;</span>);
    textarea.value = <span class="hljs-string">&apos;Error code: &apos;</span> + code;
}

<span class="hljs-keyword">var</span> request = <span class="hljs-keyword">new</span> XMLHttpRequest(); <span class="hljs-comment">// &#x65B0;&#x5EFA;XMLHttpRequest&#x5BF9;&#x8C61;</span>

request.onreadystatechange = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{ <span class="hljs-comment">// &#x72B6;&#x6001;&#x53D1;&#x751F;&#x53D8;&#x5316;&#x65F6;&#xFF0C;&#x51FD;&#x6570;&#x88AB;&#x56DE;&#x8C03;</span>
    <span class="hljs-keyword">if</span> (request.readyState === <span class="hljs-number">4</span>) { <span class="hljs-comment">// &#x6210;&#x529F;&#x5B8C;&#x6210;</span>
        <span class="hljs-comment">// &#x5224;&#x65AD;&#x54CD;&#x5E94;&#x7ED3;&#x679C;:</span>
        <span class="hljs-keyword">if</span> (request.status === <span class="hljs-number">200</span>) {
            <span class="hljs-comment">// &#x6210;&#x529F;&#xFF0C;&#x901A;&#x8FC7;responseText&#x62FF;&#x5230;&#x54CD;&#x5E94;&#x7684;&#x6587;&#x672C;:</span>
            <span class="hljs-keyword">return</span> success(request.responseText);
        } <span class="hljs-keyword">else</span> {
            <span class="hljs-comment">// &#x5931;&#x8D25;&#xFF0C;&#x6839;&#x636E;&#x54CD;&#x5E94;&#x7801;&#x5224;&#x65AD;&#x5931;&#x8D25;&#x539F;&#x56E0;:</span>
            <span class="hljs-keyword">return</span> fail(request.status);
        }
    } <span class="hljs-keyword">else</span> {
        <span class="hljs-comment">// HTTP&#x8BF7;&#x6C42;&#x8FD8;&#x5728;&#x7EE7;&#x7EED;...</span>
    }
}

<span class="hljs-comment">// &#x53D1;&#x9001;&#x8BF7;&#x6C42;:</span>
request.open(<span class="hljs-string">&apos;GET&apos;</span>, <span class="hljs-string">&apos;/api/categories&apos;</span>);
request.send();

alert(<span class="hljs-string">&apos;&#x8BF7;&#x6C42;&#x5DF2;&#x53D1;&#x9001;&#xFF0C;&#x8BF7;&#x7B49;&#x5F85;&#x54CD;&#x5E94;...&apos;</span>);
</code></pre>
<p>&#x5BF9;&#x4E8E;&#x4F4E;&#x7248;&#x672C;&#x7684;IE&#xFF0C;&#x9700;&#x8981;&#x6362;&#x4E00;&#x4E2A;<code>ActiveXObject</code>&#x5BF9;&#x8C61;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> request = <span class="hljs-keyword">new</span> ActiveXObject(<span class="hljs-string">&apos;Microsoft.XMLHTTP&apos;</span>); <span class="hljs-comment">// &#x65B0;&#x5EFA;Microsoft.XMLHTTP&#x5BF9;&#x8C61;</span>
</code></pre>
<p>&#x5982;&#x679C;&#x4F60;&#x60F3;&#x628A;&#x6807;&#x51C6;&#x5199;&#x6CD5;&#x548C;IE&#x5199;&#x6CD5;&#x6DF7;&#x5728;&#x4E00;&#x8D77;&#xFF0C;&#x53EF;&#x4EE5;&#x8FD9;&#x4E48;&#x5199;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> request;
<span class="hljs-keyword">if</span> (<span class="hljs-built_in">window</span>.XMLHttpRequest) {
    request = <span class="hljs-keyword">new</span> XMLHttpRequest();
} <span class="hljs-keyword">else</span> {
    request = <span class="hljs-keyword">new</span> ActiveXObject(<span class="hljs-string">&apos;Microsoft.XMLHTTP&apos;</span>);
}
</code></pre>
<p>&#x901A;&#x8FC7;&#x68C0;&#x6D4B;<code>window</code>&#x5BF9;&#x8C61;&#x662F;&#x5426;&#x6709;<code>XMLHttpRequest</code>&#x5C5E;&#x6027;&#x6765;&#x786E;&#x5B9A;&#x6D4F;&#x89C8;&#x5668;&#x662F;&#x5426;&#x652F;&#x6301;&#x6807;&#x51C6;&#x7684;<code>XMLHttpRequest</code>&#x3002;&#x6CE8;&#x610F;&#xFF0C;<em>&#x4E0D;&#x8981;</em>&#x6839;&#x636E;&#x6D4F;&#x89C8;&#x5668;&#x7684;<code>navigator.userAgent</code>&#x6765;&#x68C0;&#x6D4B;&#x6D4F;&#x89C8;&#x5668;&#x662F;&#x5426;&#x652F;&#x6301;&#x67D0;&#x4E2A;JavaScript&#x7279;&#x6027;&#xFF0C;&#x4E00;&#x662F;&#x56E0;&#x4E3A;&#x8FD9;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x672C;&#x8EAB;&#x53EF;&#x4EE5;&#x4F2A;&#x9020;&#xFF0C;&#x4E8C;&#x662F;&#x901A;&#x8FC7;IE&#x7248;&#x672C;&#x5224;&#x65AD;JavaScript&#x7279;&#x6027;&#x5C06;&#x975E;&#x5E38;&#x590D;&#x6742;&#x3002;</p>
<p>&#x5F53;&#x521B;&#x5EFA;&#x4E86;<code>XMLHttpRequest</code>&#x5BF9;&#x8C61;&#x540E;&#xFF0C;&#x8981;&#x5148;&#x8BBE;&#x7F6E;<code>onreadystatechange</code>&#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x3002;&#x5728;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#x4E2D;&#xFF0C;&#x901A;&#x5E38;&#x6211;&#x4EEC;&#x53EA;&#x9700;&#x901A;&#x8FC7;<code>readyState === 4</code>&#x5224;&#x65AD;&#x8BF7;&#x6C42;&#x662F;&#x5426;&#x5B8C;&#x6210;&#xFF0C;&#x5982;&#x679C;&#x5DF2;&#x5B8C;&#x6210;&#xFF0C;&#x518D;&#x6839;&#x636E;<code>status === 200</code>&#x5224;&#x65AD;&#x662F;&#x5426;&#x662F;&#x4E00;&#x4E2A;&#x6210;&#x529F;&#x7684;&#x54CD;&#x5E94;&#x3002;</p>
<p><code>XMLHttpRequest</code>&#x5BF9;&#x8C61;&#x7684;<code>open()</code>&#x65B9;&#x6CD5;&#x6709;3&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x6307;&#x5B9A;&#x662F;<code>GET</code>&#x8FD8;&#x662F;<code>POST</code>&#xFF0C;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x6307;&#x5B9A;URL&#x5730;&#x5740;&#xFF0C;&#x7B2C;&#x4E09;&#x4E2A;&#x53C2;&#x6570;&#x6307;&#x5B9A;&#x662F;&#x5426;&#x4F7F;&#x7528;&#x5F02;&#x6B65;&#xFF0C;&#x9ED8;&#x8BA4;&#x662F;<code>true</code>&#xFF0C;&#x6240;&#x4EE5;&#x4E0D;&#x7528;&#x5199;&#x3002;</p>
<p><strong><font color="#dc2836">&#x6CE8;&#x610F;</font></strong>&#xFF0C;&#x5343;&#x4E07;&#x4E0D;&#x8981;&#x628A;&#x7B2C;&#x4E09;&#x4E2A;&#x53C2;&#x6570;&#x6307;&#x5B9A;&#x4E3A;<code>false</code>&#xFF0C;&#x5426;&#x5219;&#x6D4F;&#x89C8;&#x5668;&#x5C06;&#x505C;&#x6B62;&#x54CD;&#x5E94;&#xFF0C;&#x76F4;&#x5230;AJAX&#x8BF7;&#x6C42;&#x5B8C;&#x6210;&#x3002;&#x5982;&#x679C;&#x8FD9;&#x4E2A;&#x8BF7;&#x6C42;&#x8017;&#x65F6;10&#x79D2;&#xFF0C;&#x90A3;&#x4E48;10&#x79D2;&#x5185;&#x4F60;&#x4F1A;&#x53D1;&#x73B0;&#x6D4F;&#x89C8;&#x5668;&#x5904;&#x4E8E;&#x201C;&#x5047;&#x6B7B;&#x201D;&#x72B6;&#x6001;&#x3002;</p>
<p>&#x6700;&#x540E;&#x8C03;&#x7528;<code>send()</code>&#x65B9;&#x6CD5;&#x624D;&#x771F;&#x6B63;&#x53D1;&#x9001;&#x8BF7;&#x6C42;&#x3002;<code>GET</code>&#x8BF7;&#x6C42;&#x4E0D;&#x9700;&#x8981;&#x53C2;&#x6570;&#xFF0C;<code>POST</code>&#x8BF7;&#x6C42;&#x9700;&#x8981;&#x628A;body&#x90E8;&#x5206;&#x4EE5;&#x5B57;&#x7B26;&#x4E32;&#x6216;&#x8005;<code>FormData</code>&#x5BF9;&#x8C61;&#x4F20;&#x8FDB;&#x53BB;&#x3002;</p>
<h3 id="&#x5B89;&#x5168;&#x9650;&#x5236;">&#x5B89;&#x5168;&#x9650;&#x5236;</h3>
<p>&#x4E0A;&#x9762;&#x4EE3;&#x7801;&#x7684;URL&#x4F7F;&#x7528;&#x7684;&#x662F;&#x76F8;&#x5BF9;&#x8DEF;&#x5F84;&#x3002;&#x5982;&#x679C;&#x4F60;&#x628A;&#x5B83;&#x6539;&#x4E3A;<code>&apos;http://www.sina.com.cn/&apos;</code>&#xFF0C;&#x518D;&#x8FD0;&#x884C;&#xFF0C;&#x80AF;&#x5B9A;&#x62A5;&#x9519;&#x3002;&#x5728;Chrome&#x7684;&#x63A7;&#x5236;&#x53F0;&#x91CC;&#xFF0C;&#x8FD8;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x9519;&#x8BEF;&#x4FE1;&#x606F;&#x3002;</p>
<p>&#x8FD9;&#x662F;&#x56E0;&#x4E3A;&#x6D4F;&#x89C8;&#x5668;&#x7684;&#x540C;&#x6E90;&#x7B56;&#x7565;&#x5BFC;&#x81F4;&#x7684;&#x3002;&#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;JavaScript&#x5728;&#x53D1;&#x9001;AJAX&#x8BF7;&#x6C42;&#x65F6;&#xFF0C;URL&#x7684;&#x57DF;&#x540D;&#x5FC5;&#x987B;&#x548C;&#x5F53;&#x524D;&#x9875;&#x9762;&#x5B8C;&#x5168;&#x4E00;&#x81F4;&#x3002;</p>
<p>&#x5B8C;&#x5168;&#x4E00;&#x81F4;&#x7684;&#x610F;&#x601D;&#x662F;&#xFF0C;&#x57DF;&#x540D;&#x8981;&#x76F8;&#x540C;&#xFF08;<code>www.example.com</code>&#x548C;<code>example.com</code>&#x4E0D;&#x540C;&#xFF09;&#xFF0C;&#x534F;&#x8BAE;&#x8981;&#x76F8;&#x540C;&#xFF08;<code>http</code>&#x548C;<code>https</code>&#x4E0D;&#x540C;&#xFF09;&#xFF0C;&#x7AEF;&#x53E3;&#x53F7;&#x8981;&#x76F8;&#x540C;&#xFF08;&#x9ED8;&#x8BA4;&#x662F;<code>:80</code>&#x7AEF;&#x53E3;&#xFF0C;&#x5B83;&#x548C;<code>:8080</code>&#x5C31;&#x4E0D;&#x540C;&#xFF09;&#x3002;&#x6709;&#x7684;&#x6D4F;&#x89C8;&#x5668;&#x53E3;&#x5B50;&#x677E;&#x4E00;&#x70B9;&#xFF0C;&#x5141;&#x8BB8;&#x7AEF;&#x53E3;&#x4E0D;&#x540C;&#xFF0C;&#x5927;&#x591A;&#x6570;&#x6D4F;&#x89C8;&#x5668;&#x90FD;&#x4F1A;&#x4E25;&#x683C;&#x9075;&#x5B88;&#x8FD9;&#x4E2A;&#x9650;&#x5236;&#x3002;</p>
<p>&#x90A3;&#x662F;&#x4E0D;&#x662F;&#x7528;JavaScript&#x65E0;&#x6CD5;&#x8BF7;&#x6C42;&#x5916;&#x57DF;&#xFF08;&#x5C31;&#x662F;&#x5176;&#x4ED6;&#x7F51;&#x7AD9;&#xFF09;&#x7684;URL&#x4E86;&#x5462;&#xFF1F;&#x65B9;&#x6CD5;&#x8FD8;&#x662F;&#x6709;&#x7684;&#xFF0C;&#x5927;&#x6982;&#x6709;&#x8FD9;&#x4E48;&#x51E0;&#x79CD;&#xFF1A;</p>
<p>&#x4E00;&#x662F;&#x901A;&#x8FC7;Flash&#x63D2;&#x4EF6;&#x53D1;&#x9001;HTTP&#x8BF7;&#x6C42;&#xFF0C;&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#x53EF;&#x4EE5;&#x7ED5;&#x8FC7;&#x6D4F;&#x89C8;&#x5668;&#x7684;&#x5B89;&#x5168;&#x9650;&#x5236;&#xFF0C;&#x4F46;&#x5FC5;&#x987B;&#x5B89;&#x88C5;Flash&#xFF0C;&#x5E76;&#x4E14;&#x8DDF;Flash&#x4EA4;&#x4E92;&#x3002;&#x4E0D;&#x8FC7;Flash&#x7528;&#x8D77;&#x6765;&#x9EBB;&#x70E6;&#xFF0C;&#x800C;&#x4E14;&#x73B0;&#x5728;&#x7528;&#x5F97;&#x4E5F;&#x8D8A;&#x6765;&#x8D8A;&#x5C11;&#x4E86;&#x3002;</p>
<p>&#x4E8C;&#x662F;&#x901A;&#x8FC7;&#x5728;&#x540C;&#x6E90;&#x57DF;&#x540D;&#x4E0B;&#x67B6;&#x8BBE;&#x4E00;&#x4E2A;&#x4EE3;&#x7406;&#x670D;&#x52A1;&#x5668;&#x6765;&#x8F6C;&#x53D1;&#xFF0C;JavaScript&#x8D1F;&#x8D23;&#x628A;&#x8BF7;&#x6C42;&#x53D1;&#x9001;&#x5230;&#x4EE3;&#x7406;&#x670D;&#x52A1;&#x5668;&#xFF1A;</p>
<pre><code>&apos;/proxy?url=http://www.sina.com.cn&apos;
</code></pre><p>&#x4EE3;&#x7406;&#x670D;&#x52A1;&#x5668;&#x518D;&#x628A;&#x7ED3;&#x679C;&#x8FD4;&#x56DE;&#xFF0C;&#x8FD9;&#x6837;&#x5C31;&#x9075;&#x5B88;&#x4E86;&#x6D4F;&#x89C8;&#x5668;&#x7684;&#x540C;&#x6E90;&#x7B56;&#x7565;&#x3002;&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#x9EBB;&#x70E6;&#x4E4B;&#x5904;&#x5728;&#x4E8E;&#x9700;&#x8981;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x989D;&#x5916;&#x505A;&#x5F00;&#x53D1;&#x3002;</p>
<p>&#x7B2C;&#x4E09;&#x79CD;&#x65B9;&#x5F0F;&#x79F0;&#x4E3A;JSONP&#xFF0C;&#x5B83;&#x6709;&#x4E2A;&#x9650;&#x5236;&#xFF0C;&#x53EA;&#x80FD;&#x7528;GET&#x8BF7;&#x6C42;&#xFF0C;&#x5E76;&#x4E14;&#x8981;&#x6C42;&#x8FD4;&#x56DE;JavaScript&#x3002;&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#x8DE8;&#x57DF;&#x5B9E;&#x9645;&#x4E0A;&#x662F;&#x5229;&#x7528;&#x4E86;&#x6D4F;&#x89C8;&#x5668;&#x5141;&#x8BB8;&#x8DE8;&#x57DF;&#x5F15;&#x7528;JavaScript&#x8D44;&#x6E90;&#xFF1A;</p>
<pre><code class="lang-javascript">&lt;html&gt;
&lt;head&gt;
    &lt;script src=&quot;http://example.com/abc.js&quot;&gt;&lt;/script&gt;
    ...
&lt;/head&gt;
&lt;body&gt;
...
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p>JSONP&#x901A;&#x5E38;&#x4EE5;&#x51FD;&#x6570;&#x8C03;&#x7528;&#x7684;&#x5F62;&#x5F0F;&#x8FD4;&#x56DE;&#xFF0C;&#x4F8B;&#x5982;&#xFF0C;&#x8FD4;&#x56DE;JavaScript&#x5185;&#x5BB9;&#x5982;&#x4E0B;&#xFF1A;</p>
<pre><code>foo(&apos;data&apos;);
</code></pre><p>&#x8FD9;&#x6837;&#x4E00;&#x6765;&#xFF0C;&#x6211;&#x4EEC;&#x5982;&#x679C;&#x5728;&#x9875;&#x9762;&#x4E2D;&#x5148;&#x51C6;&#x5907;&#x597D;<code>foo()</code>&#x51FD;&#x6570;&#xFF0C;&#x7136;&#x540E;&#x7ED9;&#x9875;&#x9762;&#x52A8;&#x6001;&#x52A0;&#x4E00;&#x4E2A;<code>&lt;script&gt;</code>&#x8282;&#x70B9;&#xFF0C;&#x76F8;&#x5F53;&#x4E8E;&#x52A8;&#x6001;&#x8BFB;&#x53D6;&#x5916;&#x57DF;&#x7684;JavaScript&#x8D44;&#x6E90;&#xFF0C;&#x6700;&#x540E;&#x5C31;&#x7B49;&#x7740;&#x63A5;&#x6536;&#x56DE;&#x8C03;&#x4E86;&#x3002;</p>
<p>&#x4EE5;163&#x7684;&#x80A1;&#x7968;&#x67E5;&#x8BE2;URL&#x4E3A;&#x4F8B;&#xFF0C;&#x5BF9;&#x4E8E;URL&#xFF1A;<a href="http://api.money.126.net/data/feed/0000001,1399001?callback=refreshPrice&#xFF0C;&#x4F60;&#x5C06;&#x5F97;&#x5230;&#x5982;&#x4E0B;&#x8FD4;&#x56DE;&#xFF1A;" target="_blank">http://api.money.126.net/data/feed/0000001,1399001?callback=refreshPrice&#xFF0C;&#x4F60;&#x5C06;&#x5F97;&#x5230;&#x5982;&#x4E0B;&#x8FD4;&#x56DE;&#xFF1A;</a></p>
<pre><code class="lang-json">refreshPrice({<span class="hljs-string">&quot;0000001&quot;</span>:{<span class="hljs-string">&quot;code&quot;</span>: <span class="hljs-string">&quot;0000001&quot;</span>, ... });
</code></pre>
<p>&#x56E0;&#x6B64;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x9996;&#x5148;&#x5728;&#x9875;&#x9762;&#x4E2D;&#x51C6;&#x5907;&#x597D;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">refreshPrice</span>(<span class="hljs-params">data</span>) </span>{
    <span class="hljs-keyword">var</span> p = <span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">&apos;test-jsonp&apos;</span>);
    p.innerHTML = <span class="hljs-string">&apos;&#x5F53;&#x524D;&#x4EF7;&#x683C;&#xFF1A;&apos;</span> +
        data[<span class="hljs-string">&apos;0000001&apos;</span>].name +<span class="hljs-string">&apos;: &apos;</span> + 
        data[<span class="hljs-string">&apos;0000001&apos;</span>].price + <span class="hljs-string">&apos;&#xFF1B;&apos;</span> +
        data[<span class="hljs-string">&apos;1399001&apos;</span>].name + <span class="hljs-string">&apos;: &apos;</span> +
        data[<span class="hljs-string">&apos;1399001&apos;</span>].price;
}
</code></pre>
<p>&#x6700;&#x540E;&#x7528;<code>getPrice()</code>&#x51FD;&#x6570;&#x89E6;&#x53D1;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getPrice</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">var</span>
        js = <span class="hljs-built_in">document</span>.createElement(<span class="hljs-string">&apos;script&apos;</span>),
        head = <span class="hljs-built_in">document</span>.getElementsByTagName(<span class="hljs-string">&apos;head&apos;</span>)[<span class="hljs-number">0</span>];
    js.src = <span class="hljs-string">&apos;http://api.money.126.net/data/feed/0000001,1399001?callback=refreshPrice&apos;</span>;
    head.appendChild(js);
}
</code></pre>
<p>&#x5C31;&#x5B8C;&#x6210;&#x4E86;&#x8DE8;&#x57DF;&#x52A0;&#x8F7D;&#x6570;&#x636E;&#x3002;</p>
<h3 id="cors">CORS</h3>
<p>&#x5982;&#x679C;&#x6D4F;&#x89C8;&#x5668;&#x652F;&#x6301;HTML5&#xFF0C;&#x90A3;&#x4E48;&#x5C31;&#x53EF;&#x4EE5;&#x4E00;&#x52B3;&#x6C38;&#x9038;&#x5730;&#x4F7F;&#x7528;&#x65B0;&#x7684;&#x8DE8;&#x57DF;&#x7B56;&#x7565;&#xFF1A;CORS&#x4E86;&#x3002;</p>
<p>CORS&#x5168;&#x79F0;Cross-Origin Resource Sharing&#xFF0C;&#x662F;HTML5&#x89C4;&#x8303;&#x5B9A;&#x4E49;&#x7684;&#x5982;&#x4F55;&#x8DE8;&#x57DF;&#x8BBF;&#x95EE;&#x8D44;&#x6E90;&#x3002;</p>
<p>&#x4E86;&#x89E3;CORS&#x524D;&#xFF0C;&#x6211;&#x4EEC;&#x5148;&#x641E;&#x660E;&#x767D;&#x6982;&#x5FF5;&#xFF1A;</p>
<p>Origin&#x8868;&#x793A;&#x672C;&#x57DF;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x6D4F;&#x89C8;&#x5668;&#x5F53;&#x524D;&#x9875;&#x9762;&#x7684;&#x57DF;&#x3002;&#x5F53;JavaScript&#x5411;&#x5916;&#x57DF;&#xFF08;&#x5982;sina.com&#xFF09;&#x53D1;&#x8D77;&#x8BF7;&#x6C42;&#x540E;&#xFF0C;&#x6D4F;&#x89C8;&#x5668;&#x6536;&#x5230;&#x54CD;&#x5E94;&#x540E;&#xFF0C;&#x9996;&#x5148;&#x68C0;&#x67E5;<code>Access-Control-Allow-Origin</code>&#x662F;&#x5426;&#x5305;&#x542B;&#x672C;&#x57DF;&#xFF0C;&#x5982;&#x679C;&#x662F;&#xFF0C;&#x5219;&#x6B64;&#x6B21;&#x8DE8;&#x57DF;&#x8BF7;&#x6C42;&#x6210;&#x529F;&#xFF0C;&#x5982;&#x679C;&#x4E0D;&#x662F;&#xFF0C;&#x5219;&#x8BF7;&#x6C42;&#x5931;&#x8D25;&#xFF0C;JavaScript&#x5C06;&#x65E0;&#x6CD5;&#x83B7;&#x53D6;&#x5230;&#x54CD;&#x5E94;&#x7684;&#x4EFB;&#x4F55;&#x6570;&#x636E;&#x3002;</p>
<p>&#x7528;&#x4E00;&#x4E2A;&#x56FE;&#x6765;&#x8868;&#x793A;&#x5C31;&#x662F;&#xFF1A;</p>
<p><img src="https://www.liaoxuefeng.com/files/attachments/1027024093709472/l" alt="js-cors"></p>
<p>&#x5047;&#x8BBE;&#x672C;&#x57DF;&#x662F;<code>my.com</code>&#xFF0C;&#x5916;&#x57DF;&#x662F;<code>sina.com</code>&#xFF0C;&#x53EA;&#x8981;&#x54CD;&#x5E94;&#x5934;<code>Access-Control-Allow-Origin</code>&#x4E3A;<code>http://my.com</code>&#xFF0C;&#x6216;&#x8005;&#x662F;<code>*</code>&#xFF0C;&#x672C;&#x6B21;&#x8BF7;&#x6C42;&#x5C31;&#x53EF;&#x4EE5;&#x6210;&#x529F;&#x3002;</p>
<p>&#x53EF;&#x89C1;&#xFF0C;&#x8DE8;&#x57DF;&#x80FD;&#x5426;&#x6210;&#x529F;&#xFF0C;&#x53D6;&#x51B3;&#x4E8E;&#x5BF9;&#x65B9;&#x670D;&#x52A1;&#x5668;&#x662F;&#x5426;&#x613F;&#x610F;&#x7ED9;&#x4F60;&#x8BBE;&#x7F6E;&#x4E00;&#x4E2A;&#x6B63;&#x786E;&#x7684;<code>Access-Control-Allow-Origin</code>&#xFF0C;&#x51B3;&#x5B9A;&#x6743;&#x59CB;&#x7EC8;&#x5728;&#x5BF9;&#x65B9;&#x624B;&#x4E2D;&#x3002;</p>
<p>&#x4E0A;&#x9762;&#x8FD9;&#x79CD;&#x8DE8;&#x57DF;&#x8BF7;&#x6C42;&#xFF0C;&#x79F0;&#x4E4B;&#x4E3A;&#x201C;&#x7B80;&#x5355;&#x8BF7;&#x6C42;&#x201D;&#x3002;&#x7B80;&#x5355;&#x8BF7;&#x6C42;&#x5305;&#x62EC;GET&#x3001;HEAD&#x548C;POST&#xFF08;POST&#x7684;Content-Type&#x7C7B;&#x578B; &#x4EC5;&#x9650;<code>application/x-www-form-urlencoded</code>&#x3001;<code>multipart/form-data</code>&#x548C;<code>text/plain</code>&#xFF09;&#xFF0C;&#x5E76;&#x4E14;&#x4E0D;&#x80FD;&#x51FA;&#x73B0;&#x4EFB;&#x4F55;&#x81EA;&#x5B9A;&#x4E49;&#x5934;&#xFF08;&#x4F8B;&#x5982;&#xFF0C;<code>X-Custom: 12345</code>&#xFF09;&#xFF0C;&#x901A;&#x5E38;&#x80FD;&#x6EE1;&#x8DB3;90%&#x7684;&#x9700;&#x6C42;&#x3002;</p>
<p>&#x65E0;&#x8BBA;&#x4F60;&#x662F;&#x5426;&#x9700;&#x8981;&#x7528;JavaScript&#x901A;&#x8FC7;CORS&#x8DE8;&#x57DF;&#x8BF7;&#x6C42;&#x8D44;&#x6E90;&#xFF0C;&#x4F60;&#x90FD;&#x8981;&#x4E86;&#x89E3;CORS&#x7684;&#x539F;&#x7406;&#x3002;&#x6700;&#x65B0;&#x7684;&#x6D4F;&#x89C8;&#x5668;&#x5168;&#x9762;&#x652F;&#x6301;HTML5&#x3002;&#x5728;&#x5F15;&#x7528;&#x5916;&#x57DF;&#x8D44;&#x6E90;&#x65F6;&#xFF0C;&#x9664;&#x4E86;JavaScript&#x548C;CSS&#x5916;&#xFF0C;&#x90FD;&#x8981;&#x9A8C;&#x8BC1;CORS&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x5F53;&#x4F60;&#x5F15;&#x7528;&#x4E86;&#x67D0;&#x4E2A;&#x7B2C;&#x4E09;&#x65B9;CDN&#x4E0A;&#x7684;&#x5B57;&#x4F53;&#x6587;&#x4EF6;&#x65F6;&#xFF1A;</p>
<pre><code class="lang-javascript"><span class="hljs-comment">/* CSS */</span>
@font-face {
  font-family: <span class="hljs-string">&apos;FontAwesome&apos;</span>;
  src: url(<span class="hljs-string">&apos;http://cdn.com/fonts/fontawesome.ttf&apos;</span>) format(<span class="hljs-string">&apos;truetype&apos;</span>);
}
</code></pre>
<p>&#x5982;&#x679C;&#x8BE5;CDN&#x670D;&#x52A1;&#x5546;&#x672A;&#x6B63;&#x786E;&#x8BBE;&#x7F6E;<code>Access-Control-Allow-Origin</code>&#xFF0C;&#x90A3;&#x4E48;&#x6D4F;&#x89C8;&#x5668;&#x65E0;&#x6CD5;&#x52A0;&#x8F7D;&#x5B57;&#x4F53;&#x8D44;&#x6E90;&#x3002;</p>
<p>&#x5BF9;&#x4E8E;PUT&#x3001;DELETE&#x4EE5;&#x53CA;&#x5176;&#x4ED6;&#x7C7B;&#x578B;&#x5982;<code>application/json</code>&#x7684;POST&#x8BF7;&#x6C42;&#xFF0C;&#x5728;&#x53D1;&#x9001;AJAX&#x8BF7;&#x6C42;&#x4E4B;&#x524D;&#xFF0C;&#x6D4F;&#x89C8;&#x5668;&#x4F1A;&#x5148;&#x53D1;&#x9001;&#x4E00;&#x4E2A;<code>OPTIONS</code>&#x8BF7;&#x6C42;&#xFF08;&#x79F0;&#x4E3A;preflighted&#x8BF7;&#x6C42;&#xFF09;&#x5230;&#x8FD9;&#x4E2A;URL&#x4E0A;&#xFF0C;&#x8BE2;&#x95EE;&#x76EE;&#x6807;&#x670D;&#x52A1;&#x5668;&#x662F;&#x5426;&#x63A5;&#x53D7;&#xFF1A;</p>
<pre><code>OPTIONS /path/to/resource HTTP/1.1
Host: bar.com
Origin: http://my.com
Access-Control-Request-Method: POST
</code></pre><p>&#x670D;&#x52A1;&#x5668;&#x5FC5;&#x987B;&#x54CD;&#x5E94;&#x5E76;&#x660E;&#x786E;&#x6307;&#x51FA;&#x5141;&#x8BB8;&#x7684;Method&#xFF1A;</p>
<pre><code class="lang-shell">HTTP/1.1 200 OK
Access-Control-Allow-Origin: http://my.com
Access-Control-Allow-Methods: POST, GET, PUT, OPTIONS
Access-Control-Max-Age: 86400
</code></pre>
<p>&#x6D4F;&#x89C8;&#x5668;&#x786E;&#x8BA4;&#x670D;&#x52A1;&#x5668;&#x54CD;&#x5E94;&#x7684;<code>Access-Control-Allow-Methods</code>&#x5934;&#x786E;&#x5B9E;&#x5305;&#x542B;&#x5C06;&#x8981;&#x53D1;&#x9001;&#x7684;AJAX&#x8BF7;&#x6C42;&#x7684;Method&#xFF0C;&#x624D;&#x4F1A;&#x7EE7;&#x7EED;&#x53D1;&#x9001;AJAX&#xFF0C;&#x5426;&#x5219;&#xFF0C;&#x629B;&#x51FA;&#x4E00;&#x4E2A;&#x9519;&#x8BEF;&#x3002;</p>
<p>&#x7531;&#x4E8E;&#x4EE5;<code>POST</code>&#x3001;<code>PUT</code>&#x65B9;&#x5F0F;&#x4F20;&#x9001;JSON&#x683C;&#x5F0F;&#x7684;&#x6570;&#x636E;&#x5728;REST&#x4E2D;&#x5F88;&#x5E38;&#x89C1;&#xFF0C;&#x6240;&#x4EE5;&#x8981;&#x8DE8;&#x57DF;&#x6B63;&#x786E;&#x5904;&#x7406;<code>POST</code>&#x548C;<code>PUT</code>&#x8BF7;&#x6C42;&#xFF0C;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x5FC5;&#x987B;&#x6B63;&#x786E;&#x54CD;&#x5E94;<code>OPTIONS</code>&#x8BF7;&#x6C42;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="操作文件.html" class="navigation navigation-prev " aria-label="Previous page: 操作文件">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="Canvas.html" class="navigation navigation-next " aria-label="Next page: Canvas">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"AJAX","level":"1.4.6","depth":2,"next":{"title":"Canvas","level":"1.4.7","depth":2,"path":"浏览器/Canvas.md","ref":"浏览器/Canvas.md","articles":[]},"previous":{"title":"操作文件","level":"1.4.5","depth":2,"path":"浏览器/操作文件.md","ref":"浏览器/操作文件.md","articles":[]},"dir":"ltr"},"config":{"plugins":["lunr","pageview-count","chapter-fold","back-to-top-button","code","hide-element","-sharing","sharing-plus","custom-favicon"],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"chapter-fold":{},"search":{},"sharing-plus":{"qq":false,"all":["facebook","google","twitter","instapaper","linkedin","pocket","stumbleupon"],"douban":false,"facebook":true,"weibo":false,"instapaper":false,"whatsapp":false,"hatenaBookmark":false,"twitter":true,"messenger":false,"line":false,"vk":false,"pocket":true,"google":false,"viber":false,"stumbleupon":false,"qzone":false,"linkedin":false},"lunr":{"ignoreSpecialCharacters":false,"maxIndexSize":1000000},"code":{"copyButtons":true},"hide-element":{"elements":[".gitbook-link"]},"fontsettings":{"theme":"white","family":"sans","size":2},"highlight":{},"favicon":"js.ico","back-to-top-button":{},"pageview-count":{},"custom-favicon":{},"sharing":{"qq":true,"all":["douban","facebook","google","instapaper","linkedin","twitter","weibo","messenger","qq","qzone","viber","whatsapp"],"douban":true,"facebook":true,"weibo":true,"twitter":true,"pocket":true,"google":true,"qzone":true},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"Veng","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"JavaScript学习","language":"zh-hans","gitbook":"*","description":"javaScript的学习笔记"},"file":{"path":"浏览器/AJAX.md","mtime":"2020-12-10T02:10:04.990Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-12-10T03:00:51.663Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-pageview-count/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-back-to-top-button/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-code/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-hide-element/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing-plus/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

